package com.nhncorp.mojo.jumble;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Iterator;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.StringUtils;

/**
 * 
 * @author NHN Corporation
 * @version $Rev$, $Date$
 */
public abstract class AbstractMutationMojo extends AbstractJumbleMojo {
	private static final String DEFAULT_CHARSET = "UTF-8";

	protected String getString(File file, String encoding) throws IOException {
		FileInputStream fileInputStream = null;

		try {
			fileInputStream = new FileInputStream(file);
			String result = getString(fileInputStream, encoding);

			return result;
		} finally {
			if (fileInputStream != null) {
				fileInputStream.close();
			}
		}
	}

	private String getString(InputStream inputStream, String encoding) throws IOException {
		byte[] buff = new byte[512];
		StringBuffer sb = new StringBuffer();
		int read = inputStream.read(buff);

		while (read >= 0) {
			sb.append(new String(buff, 0, read, encoding));
			read = inputStream.read(buff);
		}

		return sb.toString();
	}

	private String getContentFileWithoutPath(MavenProject project, String fitnesseRoot) throws IOException {
		File contentFile = new File(project.getBasedir() + File.separator + fitnesseRoot, "content.txt");

		if (!contentFile.exists()) {
			return "";
		}

		String result = getString(contentFile, DEFAULT_CHARSET);

		if (StringUtils.isEmpty(result)) {
			return "";
		}

		StringBuffer sb = new StringBuffer();
		String[] results = result.split("\n");

		for (int i = 0; i < results.length; i++) {
			if (StringUtils.isEmpty(results[i])) {
				continue;
			}

			// comment
			if (results[i].startsWith("'''")) {
				continue;
			}

			if (results[i].startsWith("!path")) {
				continue;
			}

			sb.append(results[i] + "\n");
		}

		return sb.toString().trim();
	}

	protected File replaceContentFile(MavenProject project, Collection artifacts, String fitnesseRoot, boolean remote) throws IOException {
		StringBuffer sb = new StringBuffer();

		sb.append("\n\n");
		sb.append("'''START Auto-generated Classpath by NTAF Plugin'''");
		sb.append("\n\n");

		String originalContent = getContentFileWithoutPath(project, fitnesseRoot);

		if (StringUtils.isNotEmpty(originalContent)) {
			sb.append(originalContent);
			sb.append("\n\n");
		}

		for (Iterator iterator = artifacts.iterator(); iterator.hasNext();) {
			File artifact = (File)iterator.next();

			sb.append("!path ");
			sb.append(artifact.getAbsolutePath());
			sb.append("\n");
		}

		if (!remote) {
			sb.append("!path " + project.getBuild().getOutputDirectory() + "\n");
		}

		sb.append("\n");
		sb.append("'''END Auto-generated Classpath by NTAF Plugin'''");
		sb.append("\n\n");

		File contentFile = null;

		if (remote) {
			contentFile = File.createTempFile("content", ".txt", new File(System.getProperty("java.io.tmpdir")));
		} else {
			contentFile = new File(project.getBasedir() + File.separator + fitnesseRoot, "content.txt");
		}

		FileOutputStream fileOutputStream = new FileOutputStream(contentFile);
		fileOutputStream.write(sb.toString().getBytes());
		fileOutputStream.close();

		return contentFile;
	}

	public abstract void execute() throws MojoExecutionException, MojoFailureException;
}
